var titleData = [{field:'id', title: 'ID'},{field: 'username', title: '用户名'}]
//获取表体
var dataList = []
var colsList = [];

//初始化页面数据
var initData = function (){
    tableTitleList();
    titleData.push({checkbox:true})
    colsList.push(titleData)
    mxDataList();
    tableInfo(colsList,dataList)
}



var tableInfo = function (colsList,dataList){
    var table = layui.table;
    table.render({
        id: 'idTest',
        elem: '#demo',
        cols:  colsList,
        data: dataList,
        page: true,
        toolbar: '#toolbarDemo'
    })

    //触发事件
    table.on('toolbar(test)', function(obj){
        switch(obj.event){
            case 'add':
                layer.msg('添加');
                break;
            case 'delete':
                //获取选中的行
                var checkStatus = table.checkStatus('idTest');
                var dataMap = checkStatus.data
                var idTest = localStorage.getItem("idTest")
                idTest = JSON.parse(idTest)
                for(var j = 0;j<dataMap.length;j++){
                    idTest.forEach((item,i) => {
                        if(dataMap[j].id === item.id){
                            idTest.splice(i,1)
                        }
                    })
                }

                tableInfo(colsList,idTest)
                localStorage.setItem("idTest",JSON.stringify(idTest))
                layer.msg('删除');
                break;
            case 'update':
                layer.msg('编辑');
                break;
        };
    });
}
//获取明细数据
var mxDataList = function (){
    var masterId = localStorage.getItem("masterId")
    var recordId = localStorage.getItem("recordId")
    var secondDetailData = {}
    secondDetailData.masterId = masterId;
    secondDetailData.recordId = recordId;

    $.ajax({
        url: '/seeyon/rest/jssdk/getSencondDatail',
        data: JSON.stringify(secondDetailData),
        type: "POST",
        processData: false,
        contentType: "application/json;charset=UTF-8",
        async:false,
        success: function (data) {
            localStorage.setItem("idTest",JSON.stringify(data))
            dataList = data
        },
        error: function (e) {
            console.log("获取二级明细字段异常！！！！！！！！！")
            dataList = [];
        }
    });

    var mxDatas = []
}

//获取页面地址

var self = window.location.origin;
//跳转选择数据页面
$("#queryData").click(function (){
    layer.open({
        type: 2,
        title:'选择数据',
        content: "/seeyon/apps_res/cap/customCtrlResources/secondDetailTableResources/html/queryData.html",
        area:['1000px','500px'],
        btn: ['确认', '取消'],
        yes: function (index, layero){
            //获取缓存中选择的数据
            var idTest = localStorage.getItem("idTest")
            console.log("获取到选中行的数据："+idTest)
            dataList = JSON.parse(idTest)
            tableInfo(colsList,dataList)

            // localStorage.removeItem("idTest")
            console.log("点击了确认按钮！！！！！！！")
            layer.close(index);
        },
        btn2: function (){
            console.log("点击了取消按钮！！！！！！！")
        }
    })
})



//获取列表表头字段信息
var tableTitleList = function (){

    $.ajax({
        url: '/seeyon/rest/jssdk/getTitleList',
        type: "GET",
        processData: false,
        contentType: false,
        async:false,
        success: function (data) {
            titleData = data
        },
        error: function (e) {
            console.log("获取二级明细字段异常！！！！！！！！！")
            titleData = [];
        }
    });
}